﻿Imports Microsoft.VisualBasic
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.GData.YouTube
Imports Google.GData.Extensions.MediaRss
Imports Google.YouTube
Imports JPC.Medialibrary.UI

Namespace JPC.Medialibrary.BLL

    Public Class YouTubeManager

        Const developerKey As String = "AI39si40aPwuT2DEcaF8pNsB-dMdUJLuqXVMp0v32nq-Txa5t_t7inaEJLcdKtDQ0-CSMja1-dbZrEABnK__HkiNTo7Ep7zKPQ"
        Const clientID As String = "ytapi-JuanPabloCassain-YouTubeTest-jml0hpff-0"
        Const applicationName As String = "You Tube Test"

        Private Shared Function YTRequest() As YouTubeRequest

            Dim settings As YouTubeRequestSettings = New YouTubeRequestSettings(applicationName, clientID, developerKey)
            Return New YouTubeRequest(settings)

        End Function

        Public Shared Function GetMostViewed() As List(Of ListItem)

            Dim videoFeed As Feed(Of Video) = YTRequest.GetStandardFeed(YouTubeQuery.MostViewedVideo)
            Return GetVideoList(videoFeed)

        End Function

        Public Shared Function GetVideosByText(ByVal searchString As String) As List(Of ListItem)

            Dim query As YouTubeQuery = New YouTubeQuery(YouTubeQuery.DefaultVideoUri)
            'query.OrderBy = "viewCount"
            query.Query = searchString
            Dim videoFeed As Feed(Of Video) = YTRequest.Get(Of Video)(query)
            Return GetVideoList(videoFeed)

        End Function

        Private Shared Function GetVideoList(ByVal videoFeed As Feed(Of Video)) As List(Of ListItem)

            Dim videoList As New List(Of ListItem)

            For Each videoEntry As Video In videoFeed.Entries
                Dim item As New ListItem
                item.Text = videoEntry.Title
                item.Info = videoEntry.Description
                item.ThumbnailURL = videoEntry.Thumbnails(0).Url
                item.CommandArgument = videoEntry.VideoId
                videoList.Add(item)
            Next

            Return videoList

        End Function

    End Class

End Namespace
